package com.example.nna.voz;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

import com.example.nna.voz.page3.LoadIcon;
public class taskLoadImage{
	 static Bitmap bitmap = null;
	 LoadIcon ld;
	 int	para1;
	 int	para2;
	 int	para3;
	 public  taskLoadImage(LoadIcon l,int p1,int p2, int p3){
		 ld = l;
		 para1 = p1;
		 para2 = p2;
		 para3 = p3;
	 }

	 public static  Bitmap DownloadImage(String urlString){
			InputStream in = null;
			String urlEmo = "/images/smilies/Off/";
			String urlEmo2="/images/smilies/emos/";
			String urlEmo3 ="images/smilies/";
			String urlAvatar ="customavatars/";
			URL url;
			HttpURLConnection conn;
			if(urlString == null) return null;
			if(urlString.equals("images/statusicon/forum_old.gif")== true){
				bitmap = Voz.mAssests.IconForumOld();
			}else if(urlString.equals("images/statusicon/forum_new.gif")== true){
				bitmap = Voz.mAssests.IconForumNew();
			}else if(urlString.equals("images/statusicon/thread_new.gif")== true){
				bitmap = Voz.mAssests.IconThreadNew();
			}else if(urlString.equals("images/statusicon/thread_hot_new.gif")== true){
				bitmap = Voz.mAssests.IconThreadHotNew();
			}else if(urlString.equals("images/statusicon/thread.gif")== true){
				bitmap = Voz.mAssests.IconThread();
			}else if(urlString.equals("images/statusicon/thread_hot.gif")== true){
				bitmap = Voz.mAssests.IconThreadHot();
			}else if(urlString.equals("images/statusicon/thread_lock.gif")== true){
				bitmap = Voz.mAssests.IconThreadLock();
			}else if (urlString.equals("avatar") == true){
				bitmap = Voz.mAssests.android();
			}else if (urlString.equals("") == true){
				//bitmap = Voz.mAssests.android();
				bitmap = null;
			}else if (urlString.equals("load") == true){
				bitmap = Voz.mAssests.android();
			}else if (urlString.contains(urlEmo) == true){
				bitmap = Voz.mAssests.Emo(urlString.substring(urlEmo.length()));
			}else if (urlString.contains(urlEmo2) == true){
				bitmap = Voz.mAssests.EmoAnimated(urlString.substring(urlEmo2.length()));
			}else if (urlString.contains(urlEmo3) == true){
				bitmap = Voz.mAssests.Emo(urlString.substring(urlEmo3.length()));
			}else if (urlString.contains(urlAvatar) == true){
				try {
					url = new URL("http://vozforums.com/"+urlString);
					conn = (HttpURLConnection)url.openConnection();
					conn.setDoInput(true);
					conn.connect();
					in = conn.getInputStream();
					if(in != null){	
						BitmapFactory.Options op = new Options();
						op.inSampleSize = 2;
						bitmap = BitmapFactory.decodeStream(in,null,op);
					}else bitmap =  null;
					in.close();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}	
			}else if (urlString.subSequence(0, 4).equals("http")){
				try {
					url = new URL(urlString);
					conn = (HttpURLConnection)url.openConnection();
					conn.setDoInput(true);
					conn.connect();
					in = conn.getInputStream();
					if(in != null){	
						BitmapFactory.Options op = new Options();
						op.inSampleSize = 2;
						bitmap = BitmapFactory.decodeStream(in,null,op);
					}
					else bitmap = null;
					in.close();
					in = null;
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}	
			}else{
				bitmap = null;
			}
			return bitmap;
			
		}
}